<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>管理后台</title>
    <meta name="description" content="管理后台">
    <meta name="keywords" content="管理后台">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        /* title */
        .content-header {
            position: relative;
            padding: 15px 15px 0 15px;
            /* margin-top: 70px; */
        }
        .content-header > h1 {
            margin: 0;
            font-size: 24px;
            font-weight: normal;
        }
        .content-header > h1 > small {
            font-size: 15px;
            display: inline-block;
            padding-left: 4px;
            font-weight: 300;
        }
        .content-header > .breadcrumb {
            float: right;
            background: transparent;
            margin-top: 0;
            margin-bottom: 0;
            font-size: 12px;
            padding: 7px 5px;
            position: absolute;
            top: 20px;
            right: 10px;
            border-radius: 2px;
        }
        .app-container{
            background: #fff;
            margin: 15px 30px 15px 15px;
        }
        .box {
            position: relative;
            border-radius: 3px;
            background: #ffffff;
            border-top: 3px solid #3c8dbc;
            padding: 10px;
            margin-bottom: 20px;
            width: 100%;
            box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
        }
        .filter-container{
            padding:10px 0 15px 0;
        }
        .filter-container .el-button,.filter-container .el-input__inner{
            padding: 0 15px;
            height: 34px;
            line-height: 34px;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">

    <div class="content-header">
        <h1>网站管理<small>权限管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>网站管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>

    <div class="app-container">
        <div class="box">

            <div class="filter-container">
                <el-input placeholder="权限名称" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findSearchPage()" class="dalfBut">查询</el-button>
                <!--                        <el-button type="primary" class="butT" @click="handleCreate()">新建权限</el-button>-->
            </div>

            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column align="center" label="序号" type="index"></el-table-column>
                <el-table-column label="账号" align="center" prop="account">
                </el-table-column>
                <el-table-column label="使用期限" align="center" prop="accountExpirationDate">
                </el-table-column>
                <!--                <el-table-column label="状态" align="center" prop="status">-->
                <!--                    <template slot-scope="scope">-->
                <!--                        <span>{{ getStatusText(scope.row.status) }}</span>-->
                <!--                    </template>-->
                <!--                </el-table-column>-->
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                        <el-button size="mini" type="danger" @click="handleReset(scope.row)">重置密码</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col>
                                <el-form-item label="名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col>
                                <el-form-item label="路径" prop="path">
                                    <el-input v-model="formData.path"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col>
                                <el-form-item label="描述" prop="description">
                                    <el-input v-model="formData.description"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑" :visible.sync="dialogFormVisibleEdit">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col>
                                <el-form-item label="账户过期时间" prop="accountExpirationDate">
                                    <el-date-picker
                                            v-model="formData.accountExpirationDate"
                                            type="datetime"
                                            placeholder="选择日期和时间"
                                            format="yyyy-MM-dd HH:mm:ss"
                                            value-format="yyyy-MM-dd HH:mm:ss"
                                    />
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisibleEdit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            statusMap: {
                0: '停用',
                1: '启用',
            },
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            subjectList: [],
            tableData: {},
            fbChoiceList: [{
                content: "",
            }],
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisibleEdit: false,
            rules: {//校验规则

            }

        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            validateConfirmPassword(rule, value, callback) {
                if (value === '') {
                    callback(new Error('请输入确认密码'));
                } else if (value !== this.formData.password) {
                    callback(new Error('两次输入的密码不一致'));
                } else {
                    callback();
                }
            },
            getStatusText(status) {
                return this.statusMap[status] || '未知状态';
            },
            //编辑
            handleEdit() {
                axios.put("/users/" + this.formData.id, this.formData).then((res) => {
                    if (res.data) {
                        this.$message({
                            message: '恭喜你，编辑成功',
                            type: 'success'
                        });
                        this.dialogFormVisibleEdit = false;
                        this.findPage();
                    }
                }).catch((error) => {
                    if (error == "Error: Request failed with status code 403") {
                        this.$message.error("无操作权限");
                    }
                });
            },
            //添加
            handleAdd() {
                axios.post("/admin/permission/add", this.formData).then((res) => {
                    if (res.data.success) {
                        this.$message({
                            message: '恭喜你，添加成功',
                            type: 'success'
                        });
                        this.dialogFormVisible = false;
                        this.findPage();
                    }
                }).catch((error) => {
                    if (error == "Error: Request failed with status code 403") {
                        this.$message.error("无操作权限");
                    }
                });
            },

            findSearchPage() {
                this.pagination.currentPage = 1;
                this.findPage();
            },
            //分页查询
            findPage() {
                const params = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.get("/users", {params}).then((res) => {
                    if (res.data) {
                        this.dataList = res.data.records;
                        this.pagination.total = res.data.total;
                    }
                }).catch((error) => {
                    if (error == "Error: Request failed with status code 403") {
                        this.$message.error("无操作权限");
                    }
                });
            },
            // 重置表单
            resetForm() {
                this.formData = {};
                this.subjects = [];
                this.tableData = {};
            },
            // 弹出添加窗口
            handleCreate() {
                this.resetForm();
                this.dialogFormVisible = true;
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.resetForm();
                this.dialogFormVisibleEdit = true;
                this.formData = row;
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            handleReset(row) {
                this.$confirm('此操作将重置密码为 123456, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.put("/users/" + row.id + "/reset/").then((res) => {
                        if (res.data) {
                            this.$message.success("重置成功");
                            this.findPage();
                        }
                    }).catch((error) => {
                        if (error == "Error: Request failed with status code 403") {
                            this.$message.error("无操作权限");
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消重置'
                    });
                });

            },
            handleDelete(row) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.delete("/users/" + row.id).then((res) => {
                        if (res.data) {
                            this.$message.success(res.data);
                            this.findPage();
                        }
                    }).catch((error) => {
                        if (error == "Error: Request failed with status code 403") {
                            this.$message.error("无操作权限");
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            }
        }
    });
</script>
</html>